package com.xp.code.servicesImpl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xp.code.dao.ILoginInfoDao;
import com.xp.code.services.ILoginInfoService;
import com.xp.code.util.XPCodeSession;
import com.xp.code.vo.LoginInfoVO;

/**
 * 
 * @author HAHA
 * 
 */
@Service("loginInfoService")
@Transactional(rollbackFor = java.lang.Exception.class)
public class LoginInfoService implements ILoginInfoService {

	@Resource(name = "loginInfoDao")
	private ILoginInfoDao loginInfoDao;

	public LoginInfoService() {

	}

	public ILoginInfoDao getLoginInfoDao() {
		return loginInfoDao;
	}

	public void setLoginInfoDao(ILoginInfoDao loginInfoDao) {
		this.loginInfoDao = loginInfoDao;
	}

	public LoginInfoVO validateLogin(String username, String passwd) {
		LoginInfoVO loginInfoEntity = new LoginInfoVO();
		List<Object[]> list = loginInfoDao.validateLogin(username, passwd);
		if (list == null || list.size() < 1) {
			loginInfoEntity.setLoginCode("4");
			loginInfoEntity.setLoginMsg("没有该用户");
		} else {
			for (Object[] obj : list) {
				loginInfoEntity.setUserID(obj[0].toString());
				loginInfoEntity.setUserCode(obj[1].toString());
				loginInfoEntity.setUserName(obj[3].toString());
				loginInfoEntity.setDeptID(obj[6]!=null?obj[6].toString():"");
				loginInfoEntity.setDeptName(obj[7]!=null?obj[7].toString():"");
				loginInfoEntity.setUnitID(obj[8]!=null?obj[8].toString():"");
				loginInfoEntity.setUnitName(obj[9]!=null?obj[9].toString():"");
				loginInfoEntity.setLoginCode("0");
				loginInfoEntity.setLoginMsg("成功");
				XPCodeSession xpsession = new XPCodeSession();
				xpsession.setAttr("userID", obj[0].toString());
				xpsession.setAttr("userCode", obj[1].toString());
				xpsession.setAttr("userName", obj[3].toString());
				xpsession.setAttr("deptID", obj[6]!=null?obj[6].toString():"");
				xpsession.setAttr("deptName", obj[7]!=null?obj[7].toString():"");
//				xpsession.setAttr("unitID", obj[8]!=null?obj[8].toString():"");
				xpsession.setAttr("unitID", "E43094EC-2F09-4E31-B531-089F1E3EA1D7");
				xpsession.setAttr("unitName", obj[9]!=null?obj[9].toString():"");
				if (!obj[2].toString().equals(passwd)) {
					loginInfoEntity.setLoginCode("1");
					loginInfoEntity.setLoginMsg("密码错误");
				}
				if (obj[4].toString().equals("1")) {
					loginInfoEntity.setLoginCode("2");
					loginInfoEntity.setLoginMsg("超过有效期");
				}
				if (obj[5].toString().equals("1")) {
					loginInfoEntity.setLoginCode("3");
					loginInfoEntity.setLoginMsg("用户锁定");
				}
			}
		}
		return loginInfoEntity;
	}
}
